<?php

	 /*
	  * To change this template, choose Tools | Templates
	  * and open the template in the editor.
	  */

	 /**
	  * Description of SpecialAjaxController
	  *
	  * @author Ganda
	  */
	 class SpecialAjaxController extends Controller {

		 public function accessRules() {
			 return array(
				  array('allow',
						'actions' => array('checklist', 'requestCurrentURL', 'updateexpired', 'getLocationCompany'),
						'users' => array('@'),
				  ),
			 );
		 }

		 private function getAllowMyAccessRule($seachAction) {
			 $boleh = FALSE;
			 $myAccessRule = $this->accessRules();

			 for ($i = 0; $i < count($myAccessRule); $i++) {
				 $arr = $myAccessRule[$i];
				 $arrAction = array();
				 $arrUser = array();

				 if (is_int(array_search('allow', $arr))) {
					 $arrAction = isset($arr['actions']) ? $arr['actions'] : array();
					 $arrUser = isset($arr['users']) ? $arr['users'] : array();

					 if (is_int(array_search($seachAction, $arrAction))) {
						 if (is_int(array_search(Yii::app()->user->name, $arrUser))
						 || is_int(array_search(!Yii::app()->user->isGuest ? '@' : '', $arrUser))
						 || is_int(array_search(Yii::app()->user->isGuest ? '*' : '', $arrUser))
						 ) {
							 $boleh = TRUE;
						 }
					 }
				 }
			 }

			 return $boleh;
		 }

		 public function actionChecklist() {
			 $accRule = Sysappaccessrule::model()->findAll("MenuGroupID='{$_POST['menuGroupID']}' and PermissionAccessID = 1 and RuleTypeID = 1 and isActive = 1");

			 $a = array();

			 if ($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))) {
				 foreach ($accRule as $ar) {
					 array_push($a, array('ControllerName' => $ar->ControllerName, 'RuleValues' => $ar->RuleValues));
				 }
			 }
			 echo json_encode($a);
		 }

		 public function actionRequestCurrentURL() {
			 $cr = $_POST['cr'];
			 $co = $_POST['co'];
			 $ca = $_POST['ca'];
			 $unauthorizedURL = Appglobalparameters::model()->find("ParameterName='UNAUTHORIZED' and isActive = 1");
			 $videoNotFoundURL = Appglobalparameters::model()->find("ParameterName='VIDEO_NOT_FOUND' and isActive = 1");

			 if (isset($cr) && isset($co) && isset($ca)) {
				 $reqvid = Cctvrequestvideo::model()->find("CredentialName='{$cr}' and ComputerName = '{$co}' and CameraName = '{$ca}' and isActive = 1");

				 if (!isset($reqvid)) {
					 echo json_encode(isset($videoNotFoundURL) ? $videoNotFoundURL->Value : '');
				 } else {
					 if ($cr == Yii::app()->user->name) {
						 $usrs = new Sysusrsecurity;
						 $today = Date('Y-m-d');
						 $usrs = Sysusrsecurity::model()->find("CredentialName='{$cr}' and isLocked = '0' and ExpiredDate > '{$today}'", array('CredentialName ASD'));

						 $mappVid = new Cctvmappingcamera();
						 $mappVid = Cctvmappingcamera::model()->find("MenuGroupID = '{$usrs->MenuGroupID}' and ComputerName = '{$co}' and CameraName = '{$ca}' and isActive = 1");
						 if (isset($mappVid)) {
							 echo json_encode($reqvid->RequestURL);
						 } else {
							 echo json_encode(isset($unauthorizedURL) ? $unauthorizedURL->Value : '');
						 }
					 } else {
						 echo json_encode(isset($unauthorizedURL) ? $unauthorizedURL->Value : '');
					 }
				 }
			 }
		 }

		 public function actionUpdateexpired() {
			 if (isset($_POST['cr']) && $_POST['co'] && $_POST['ca']) {
				 $req = new Cctvrequestvideo;
				 $req = Cctvrequestvideo::model()->find("CredentialName='{$_POST['cr']}' and ComputerName = '{$_POST['co']}' and CameraName = '{$_POST['ca']}' and isActive = 1");
				 $req->RequestExpired = date("Y-m-d H:i:s", mktime(date('H'), date('i') + 2, date('s'), date('m'), date('d'), date('Y')));

				 $req->save();
			 }
		 }

		 public function actionGetLocationCompany() {
			 $getCompGroup = Sysusrsecurity::model()->find('CredentialName="' . $_GET['username'] . '"')->MenuGroupID;

			 /* $criteria1Loc = new CDbCriteria;
			   $criteria1Loc->alias = "groupapp";
			   $criteria1Loc->select = "distinct regency.RegencyName, regency.Latitude, regency.Longitude";
			   $criteria1Loc->join = "JOIN invcompany inv ON groupapp.CompanyID = inv.CompanyID";
			   $criteria1Loc->join .= " JOIN invcompanybranch branch ON branch.CompanyID = inv.CompanyID";
			   $criteria1Loc->join .= " JOIN gflocation gloc ON gloc.LocationID = branch.AddressLocationID";
			   $criteria1Loc->join .= " JOIN gfregency regency ON regency.RegencyID = gloc.RegencyID";
			   $criteria1Loc->condition = "groupapp.CompanyGroup=:compGroup and groupapp.isActive=1";
			   $criteria1Loc->params = array(":compGroup" => $getCompGroup);
			   $getDataSDM = Sysappmenugroup::model()->findAll($criteria1Loc); */
			 $sql = "select distinct regency.RegencyName, regency.Latitude, regency.Longitude from sysappmenugroup groupapp
JOIN invcompany inv ON groupapp.CompanyID = inv.CompanyID
JOIN invcompanybranch branch ON branch.CompanyID = inv.CompanyID
JOIN gflocation gloc ON gloc.LocationID = branch.AddressLocationID
JOIN gfregency regency ON regency.RegencyID = gloc.RegencyID 
where groupapp.CompanyGroup=" . $getCompGroup . " and groupapp.isActive=1
";
			 
			 $sql2 = "select gloc.LocationID,branch.CompanyBranchName, gloc.Latitude, gloc.Longitude from sysappmenugroup groupapp
JOIN invcompany inv ON groupapp.CompanyID = inv.CompanyID
JOIN invcompanybranch branch ON branch.CompanyID = inv.CompanyID
JOIN gflocation gloc ON gloc.LocationID = branch.AddressLocationID
JOIN gfregency regency ON regency.RegencyID = gloc.RegencyID 
where groupapp.CompanyGroup=" . $getCompGroup . " and groupapp.isActive=1";
			 $getDataSDMOne = Yii::app()->db->createCommand($sql)->queryAll();			 
			 $getDataSDMTwo = Yii::app()->db->createCommand($sql2)->queryAll();			 
			 $data = array();
			 foreach ($getDataSDMOne as $getAllOne) {
				 $data['one'][] = $getAllOne;
			 }
			 foreach ($getDataSDMTwo as $getAllTwo) {
				 $data['two'][] = $getAllTwo;
			 }
			 echo json_encode($data);
		 }

	 }

?>
